<template>
  <div class="linkaction">
    <van-goods-action>
      <van-goods-action-icon
        color="orange"
        size="30"
        icon="star-o"
        text="收藏"
        v-if="!collect"
        @click="collectFn"
      />
      <van-goods-action-icon
        color="orange"
        size="30"
        icon="star"
        text="已收藏"
        v-else
        @click="collectFn"
      />
      <van-goods-action-icon
        color="red"
        size="30"
        icon="star-o"
        text="点赞"
        v-if="!like"
        @click="likeFn"
      />
      <van-goods-action-icon
        color="red"
        size="30"
        icon="star"
        text="已点赞"
        v-else
        @click="likeFn"
      />
    </van-goods-action>
  </div>
</template>
<script>
export default {
  data() {
    return {
      collect: false, // 收藏
      like: false, // 喜欢
      islogin: false, //登录状态
      id: "", //电影id
      colArr: JSON.parse(localStorage.getItem("colArr")) || [], //收藏的id数组
      likeArr: JSON.parse(localStorage.getItem("likeArr")) || [], //喜欢的id数组
    };
  },
  created() {},
  mounted() {
    // 判断登录
    if (sessionStorage.getItem("user")) {
      this.islogin = true;
    } else {
      this.islogin = false;
    }
      // 拿到id
    this.id = +this.$route.params.filmId;
    // 判断有没有被收藏和喜欢
    if(this.colArr.includes(this.id)) this.collect=true
    else this.collect=false
    if(this.likeArr.includes(this.id)){
       this.like=true
       console.log('like');
    }
    else this.like = false
  },
  methods: {
    collectFn() {
      if (this.islogin) {
        this.collect = !this.collect;
        if (this.collect) {
          this.$toast("已收藏");
          this.colArr.unshift(this.id);
        } else {
          this.$toast("取消收藏");
          this.colArr = this.colArr.filter((item) => item != this.id);
        }
      } else {
        this.$toast("先去登录，宝贝");
        this.$router.push("/my");
      }
      // 存储本地
      localStorage.setItem("colArr", JSON.stringify(this.colArr));
    },
    likeFn() {
      if (this.islogin) {
        this.like = !this.like;
        if (this.like) {
          this.$toast("已点赞");
          this.likeArr.unshift(this.id);
        } else {
          this.$toast("取消点赞");
          this.likeArr = this.likeArr.filter((item) => item != this.id);
        }
      } else {
        this.$toast("先去登录，宝贝");
        this.$router.push("/my");
      }
      // 存储本地
      localStorage.setItem("likeArr", JSON.stringify(this.likeArr));
    },
  },
};
</script>
<style lang="less" scoped>
.van-goods-action {
  display: flex;
  justify-content: space-around;
}
</style>
